package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.entity.SheetInfoEntity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * FileName: CheckDescListAdapter
 * Author: XJH
 * Date: 2020/8/31 16:48
 * Description:
 * History:
 */
public class CheckDescListAdapter extends BaseQuickAdapter<SheetInfoEntity.SheetCheckTableLineEntity, BaseViewHolder> {
    private String mCheckTime = "";
    private List<String> mResultList;
    private FreeUI_GeneralPop mResultPop;


    public CheckDescListAdapter(int layoutResId, @Nullable List<SheetInfoEntity.SheetCheckTableLineEntity> data,String s) {
        super(layoutResId, data);
        this.mCheckTime = s;
        mResultList = new ArrayList<>();
        mResultList.add("OK");
        mResultList.add("NG");
    }


    @Override
    protected void convert(BaseViewHolder helper, SheetInfoEntity.SheetCheckTableLineEntity item) {
            helper.setText(R.id.tvPosition,String.valueOf(helper.getAdapterPosition() + 1));
            helper.setText(R.id.tvCheckName,item.getNAME());
            helper.setText(R.id.tvCheckDesc,item.getCRITERIA());
            EditText etValue = helper.getView(R.id.etValue);
            EditText etComment = helper.getView(R.id.etComment);
            setEditListener(etComment,item,"N");
        if (item.getDATATYPE().equals("NUM")){
                etValue.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_SIGNED|InputType.TYPE_NUMBER_FLAG_DECIMAL);
                try {
                    if(!item.getVALUE().equals("")){
                        compare(Double.parseDouble(item.getVALUE()),item.getMAXVALUE(),item.getMINVALUE(),etValue);
                    }
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
//            EditText etComment = helper.getView(R.id.etComment);
//            setEditListener(etComment,item,"N");
            }
        if (item.getDATATYPE().equals("DROP")||item.getDATATYPE().equals("PULL")){
            etValue.setVisibility(View.GONE);
//            if(mCheckTime.equals("巡检")){
//                etValue.setVisibility(View.INVISIBLE);
//            }
            if(item.getVALUE().equals("1")){
                item.setVALUE("OK");
                item.setRESERVED8("OK");
                etValue.setText("OK");
                helper.setText(R.id.tvResult,"OK");
            }
            if(item.getVALUE().equals("0")){
                item.setVALUE("NG");
                item.setRESERVED8("NG");
                etValue.setText("NG");
                helper.setText(R.id.tvResult,"NG");
            }
//            EditText etComment = helper.getView(R.id.etComment);
            FrameLayout flResultList = helper.getView(R.id.flResultList);
            flResultList.setVisibility(View.VISIBLE);
            flResultList.setOnClickListener(view -> {
                mResultPop = FreeApi_PopUtils.getNormalPop(MyApp.app, flResultList.getWidth(), mResultList, helper.getView(R.id.ivResultArrow), (code, i) -> {
                    item.setRESERVED8(code);
                    etValue.setText(code);
                    helper.setText(R.id.tvResult,code);
                    helper.getView(R.id.etComment).setFocusable(true);//设置输入框可聚焦
                    helper.getView(R.id.etComment).requestFocus();//请求聚焦
                    helper.getView(R.id.etComment).findFocus();//获取焦点
                });
                FreeApi_PopUtils.setArrowShow(helper.getView(R.id.ivResultArrow));
                mResultPop.showAsDropDown(flResultList, 0, 0);
            });
//            setEditListener(etComment,item,"N");
        }
            helper.setText(R.id.etValue,item.getVALUE());
            setEditListener(helper.getView(R.id.etValue),item,"Y");
        if (mCheckTime.equals("巡检")){
//            EditText etComment = helper.getView(R.id.etComment);
            etComment.setText(item.getCOMMENTS());
            helper.setText(R.id.tvResult,item.getRESERVED8());
//            setEditListener(etComment,item,"N");
            FrameLayout flResultList = helper.getView(R.id.flResultList);
            flResultList.setOnClickListener(view -> {
                mResultPop = FreeApi_PopUtils.getNormalPop(MyApp.app, flResultList.getWidth(), mResultList, helper.getView(R.id.ivResultArrow), (code, i) -> {
                    item.setRESERVED8(code);
                    helper.setText(R.id.tvResult,code);
                    helper.getView(R.id.etComment).setFocusable(true);//设置输入框可聚焦
                    helper.getView(R.id.etComment).requestFocus();//请求聚焦
                    helper.getView(R.id.etComment).findFocus();//获取焦点
                });
                FreeApi_PopUtils.setArrowShow(helper.getView(R.id.ivResultArrow));
                mResultPop.showAsDropDown(flResultList, 0, 0);
            });

            if(!item.getDATATYPE().equals("DROP")&&!item.getDATATYPE().equals("PULL")) {
                LinearLayout llResultList = helper.getView(R.id.llResultList);
                llResultList.setVisibility(View.GONE);
            }
        }
    }

    private void setEditListener(EditText editText, SheetInfoEntity.SheetCheckTableLineEntity item,String type) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {
                    if (type.equals("Y")){
                        if (item.getDATATYPE().equals("NUM")){
                            try {
                                double value = Double.parseDouble(s.toString());
                                compare(value,item.getMAXVALUE(),item.getMINVALUE(),editText);
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (type.equals("Y")){
                     item.setVALUE(s.toString());
                 }else {
                     item.setCOMMENTS(s.toString());
                 }
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    private void compare(double value, String maxValue,String minValue,EditText editText){
        if (!FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double maxValues = Double.parseDouble(maxValue);
            double minValues = Double.parseDouble(minValue);
            if(minValues<=value&&value<=maxValues) {
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
            }else{
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
            }
        }else if (FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
            double minValue1 = Double.parseDouble(minValue);
            if (value >= minValue1){
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
            }else{
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
            }
        }else if (!FreeApi_StringUtils.isEmpty(maxValue)&&FreeApi_StringUtils.isEmpty(minValue)){
            double maxValue1 = Double.parseDouble(maxValue);
            if (value <= maxValue1){
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
            }else{
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
            }
        }else {
            editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
        }
    }
}
